package org.epidroid.epidroid.androsoul.ui.conversation;

import org.epidroid.epidroid.androsoul.engine.AndroSoul;

import android.util.Log;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

class ConversationGestureDetector extends SimpleOnGestureListener {

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	
	private Runnable _left = null;
	private Runnable _right = null;
	
	public ConversationGestureDetector(Runnable left, Runnable right) {
		_left = left;
		_right = right;		
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		try {
			if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
				return false;
			// right to left swipe
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				Log.i(AndroSoul.LOGCAT, "left swipe");
				if (_left != null)
					_left.run();

			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				Log.i(AndroSoul.LOGCAT, "right swipe");
				if (_right != null)
					_right.run();
			}
		} catch (Exception e) {
			// nothing
		}
		return false;
	}

}
